<template>
  <view>
    <view class="title">欢迎使用</view>
    <view class="user-reg">用户登录</view>
    <form class="regForm" @submit="login">
      <view class="form-item">
        <input type="number" name="phone" value="" placeholder="手机号">
      </view>
      <view class="form-item">
        <input type="password" name="password" value="" placeholder="密码">
      </view>
      <view class="fontSet" style="margin-top: 80rpx; font-size: 30rpx;">
        <checkbox value="isDisabled" @click="checkboxChange"/>
        我已阅读并同意<text>用户手册</text>和<text>隐私政策</text>
      </view>
      <button :disabled="isDisabled" class="login" form-type="submit">登录</button>
    </form>

    <view class="loginFontSet">还没账号？<text @click="goRegister">立即注册</text></view>
  </view>
</template>

<script>
  import { userLogin } from '../../request/api.js'
  export default {
    data() {
      return {
        isDisabled: true,
        info: {
          phoneNum: '',
          password: ''
        }
      }
    },
    methods: {
      checkboxChange(){
        this.isDisabled = !this.isDisabled
      },
      goRegister() {
        uni.navigateTo({
          url: '/pages/register/register'
        })
      },
      login(e){
        this.info.phoneNum = e.detail.value.phone
        this.info.password = e.detail.value.password
        
        userLogin(this.info).then((res) => {
          // console.log('cg', res)
          uni.setStorageSync('token',res.token)
          uni.setStorageSync('userInfo',res.userInfo)
          uni.showToast({
            icon: 'success',
            title: '登录成功！'
          })
        }).catch((err) => {
          console.log('失败',err)
          uni.showToast({
            icon: 'error',
            title: '登录失败！'
          })
        })
        setTimeout(() => {
          uni.switchTab({
            url: '/pages/my/my'
          })
        },500)
      }
    }
  }
</script>

<style>
  page {
    background-image: linear-gradient(#B3D0EE, #E3EDF9);
  }

  .title {
    font-size: 48rpx;
    font-weight: 550;
    margin: 30rpx 0 50rpx 5%;
  }

  .user-reg {
    font-size: 36rpx;
    font-weight: 550;
    margin-left: 5%;
    margin-bottom: 40rpx;
  }

  .regForm {
    width: 90%;
    display: block;
    margin: 0 auto;
    padding: 10rpx 0 40rpx 0;
    box-sizing: border-box;
  }

  .login {
    width: 90%;
    border-radius: 100rpx;
    background: #a6c5fc;
    color: #fff;
    margin-top: 40rpx;
  }

  .form-item {
    width: 100%;
  }

  .form-item input {
    width: 100%;
    height: 100rpx;
    padding: 0 30rpx;
    box-sizing: border-box;
    background-color: white;
    margin-bottom: 24rpx;
    border-radius: 60rpx;
  }

  view text {
    color: #98a4f8;
  }

  .fontSet {
    margin-left: 5%;
    color: #979cae;
  }

  .loginFontSet {
    margin-top: 30rpx;
    text-align: center;
  }
</style>